package com.shop2cn.iapi.member.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.member.parameter.CreateMemberBean;
import com.shop2cn.iapi.member.parameter.ProcessOrderPayBean;
import com.shop2cn.iapi.member.service.ProcessOrderPayCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmember.Member;
import com.ymttest.database.model.godmember.MemberPoints;
import com.ymttest.database.model.godmember.PointsEntry;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.database.sqlwapper.godmember.GodMemberWapper;
/**
*接收订单支付消息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ProcessOrderPay{
	private static ProcessOrderPayBean processorderpayBean;
	private static ProcessOrderPayCall processorderpayCall;
	private static Integer shopid ;
	GodMemberWapper GodMemberWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("接收订单支付消息");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		processorderpayBean=new ProcessOrderPayBean();
		processorderpayCall=new ProcessOrderPayCall();
		GodMemberWapper=new GodMemberWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(processorderpayCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProcessOrderPay_001() {
		Logger.start(true,"接收订单支付消息成功,积分增加");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			processorderpayBean.setBuyerId(userId);
			processorderpayBean.setSqOrderRmbAmount("150");
			processorderpayBean.setOrderId(orderid);
			processorderpayBean.setSellerId(shopid);
			processorderpayBean.setPayTime(YMTDateUtil.getBeforeOrNextDay(0));
			processorderpayBean.setGoodsOwnerId(shopid);
			processorderpayCall.setData(processorderpayBean);
			processorderpayCall.callService();			
			Logger.verifyEquals("ok", processorderpayCall.getReturnData(), "返回成功");
			

			MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
//			{"memberId":"HY20200521001322","shopId":500962122,"availablePoints":10,"totalPoints":10,"orderNum":0,"orderAmt":0.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":0.00,"lastOrderId":0,"createTime":"2020-05-21T18:45:52","updateTime":"2020-05-21T18:45:52"}
			Logger.verifyEquals( 5,mpdb.getAvailablePoints(), "验证getAvailablePoints");
			Logger.verifyEquals(processorderpayBean.getSellerId(), mpdb.getShopId(), "验证getShopId");
			Logger.verifyEquals(5,mpdb.getTotalPoints(), "验证getTotalPoints");
			Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			PointsEntry entrydb = Entrylist.stream().filter(x->{return x.getBizType().equals(100000);}).findFirst().get();
//			[{"entryId":63,"memberId":"HY20200521001326","bizType":200000,"bizId":"c62523d2d75e466493ef30c28e58873e","changePoints":10,"points":10,"pointsTime":"2020-05-21T18:50:54","operator":"主账号","remark":"test932","createTime":"2020-05-21T18:50:54","updateTime":"2020-05-21T18:50:54"}]
			Logger.verifyEquals(5,entrydb.getChangePoints(), "验证ChangePoints");
			Logger.verifyEquals(5,entrydb.getPoints(),  "验证points");
			Logger.verifyEquals("系统",entrydb.getOperator(),  "验证Operator");
			Logger.verifyEquals("下单送积分",entrydb.getRemark(),  "验证remark");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ProcessOrderPay_002() {
		Logger.start(true,"店铺不存在,不处理");
		try {
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			processorderpayBean.setBuyerId(userId);
			processorderpayBean.setSqOrderRmbAmount("150");
			processorderpayBean.setOrderId(1l);
			processorderpayBean.setSellerId(shopid+1);
			processorderpayBean.setPayTime(YMTDateUtil.getBeforeOrNextDay(0));
			processorderpayBean.setGoodsOwnerId(shopid);
			processorderpayCall.setData(processorderpayBean);
			processorderpayCall.callService();			
			Logger.verifyEquals("ok", processorderpayCall.getReturnData(), "返回成功");
			

			int size= GodMemberWapper.selectMemberPointsByMemberId(memid).size();
			Logger.verifyEquals(0, size, "返回size0");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ProcessOrderPay_003() {
		Logger.start(true,"用户不存在,不处理");
		try {
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			processorderpayBean.setBuyerId(userId+1);
			processorderpayBean.setSqOrderRmbAmount("150");
			processorderpayBean.setOrderId(1l);
			processorderpayBean.setSellerId(shopid);
			processorderpayBean.setPayTime(YMTDateUtil.getBeforeOrNextDay(0));
			processorderpayBean.setGoodsOwnerId(shopid);
			processorderpayCall.setData(processorderpayBean);
			processorderpayCall.callService();			
			Logger.verifyEquals("ok", processorderpayCall.getReturnData(), "返回成功");
			

			int size= GodMemberWapper.selectMemberPointsByMemberId(memid).size();
			Logger.verifyEquals(0, size, "返回size0");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ProcessOrderPay_004() {
		Logger.start(true,"金额不足最小规则,验证积分");
		try {
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			processorderpayBean.setBuyerId(userId);
			processorderpayBean.setSqOrderRmbAmount("99");
			processorderpayBean.setOrderId(orderid);
			processorderpayBean.setSellerId(shopid);
			processorderpayBean.setPayTime(YMTDateUtil.getBeforeOrNextDay(0));
			processorderpayBean.setGoodsOwnerId(shopid);
			processorderpayCall.setData(processorderpayBean);
			processorderpayCall.callService();			
			Logger.verifyEquals("ok", processorderpayCall.getReturnData(), "返回成功");
			

			MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
//			{"memberId":"HY20200521001322","shopId":500962122,"availablePoints":10,"totalPoints":10,"orderNum":0,"orderAmt":0.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":0.00,"lastOrderId":0,"createTime":"2020-05-21T18:45:52","updateTime":"2020-05-21T18:45:52"}
			Logger.verifyEquals( 0,mpdb.getAvailablePoints(), "验证getAvailablePoints");
			Logger.verifyEquals(processorderpayBean.getSellerId(), mpdb.getShopId(), "验证getShopId");
			Logger.verifyEquals(0,mpdb.getTotalPoints(), "验证getTotalPoints");
			Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			Boolean isPresent= Entrylist.stream().filter(x->{return x.getBizType().equals(100000);}).findFirst().isPresent();
			Logger.verifyEquals(false, isPresent, "验证没有流水记录");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ProcessOrderPay_005() {
		Logger.start(true,"金额为门槛的多倍,验证积分");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			processorderpayBean.setBuyerId(userId);
			processorderpayBean.setSqOrderRmbAmount("1000");
			processorderpayBean.setOrderId(orderid);
			processorderpayBean.setSellerId(shopid);
			processorderpayBean.setPayTime(YMTDateUtil.getBeforeOrNextDay(0));
			processorderpayBean.setGoodsOwnerId(shopid);
			processorderpayCall.setData(processorderpayBean);
			processorderpayCall.callService();			
			Logger.verifyEquals("ok", processorderpayCall.getReturnData(), "返回成功");
			

			MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
//			{"memberId":"HY20200521001322","shopId":500962122,"availablePoints":10,"totalPoints":10,"orderNum":0,"orderAmt":0.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":0.00,"lastOrderId":0,"createTime":"2020-05-21T18:45:52","updateTime":"2020-05-21T18:45:52"}
			Logger.verifyEquals( 50,mpdb.getAvailablePoints(), "验证getAvailablePoints");
			Logger.verifyEquals(processorderpayBean.getSellerId(), mpdb.getShopId(), "验证getShopId");
			Logger.verifyEquals(50,mpdb.getTotalPoints(), "验证getTotalPoints");
			Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			PointsEntry entrydb = Entrylist.stream().filter(x->{return x.getBizType().equals(100000);}).findFirst().get();
//			[{"entryId":63,"memberId":"HY20200521001326","bizType":200000,"bizId":"c62523d2d75e466493ef30c28e58873e","changePoints":10,"points":10,"pointsTime":"2020-05-21T18:50:54","operator":"主账号","remark":"test932","createTime":"2020-05-21T18:50:54","updateTime":"2020-05-21T18:50:54"}]
			Logger.verifyEquals(50,entrydb.getChangePoints(), "验证ChangePoints");
			Logger.verifyEquals(50,entrydb.getPoints(),  "验证points");
			Logger.verifyEquals("系统",entrydb.getOperator(),  "验证Operator");
			Logger.verifyEquals("下单送积分",entrydb.getRemark(),  "验证remark");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ProcessOrderPay_006() {
		Logger.start(true,"商家没有设置过积分规则");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
//			GodMemberService.mchSetOpenPoints(GodMemberService.initSetPointsBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			processorderpayBean.setBuyerId(userId);
			processorderpayBean.setSqOrderRmbAmount("1000");
			processorderpayBean.setOrderId(orderid);
			processorderpayBean.setSellerId(shopid);
			processorderpayBean.setPayTime(YMTDateUtil.getBeforeOrNextDay(0));
			processorderpayBean.setGoodsOwnerId(shopid);
			processorderpayCall.setData(processorderpayBean);
			processorderpayCall.callService();			
			Logger.verifyEquals("ok", processorderpayCall.getReturnData(), "返回成功");
			
			MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
			//没设置过积分规则-没开启积分 不会有积分变动 不会进入流水
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
//			{"memberId":"HY20200521001322","shopId":500962122,"availablePoints":10,"totalPoints":10,"orderNum":0,"orderAmt":0.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":0.00,"lastOrderId":0,"createTime":"2020-05-21T18:45:52","updateTime":"2020-05-21T18:45:52"}
			Logger.verifyEquals( 0,mpdb.getAvailablePoints(), "验证getAvailablePoints");
			Logger.verifyEquals(processorderpayBean.getSellerId(), mpdb.getShopId(), "验证getShopId");
			Logger.verifyEquals(0,mpdb.getTotalPoints(), "验证getTotalPoints");
			Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			Boolean isPresent= Entrylist.stream().filter(x->{return x.getBizType().equals(100000);}).findFirst().isPresent();
			Logger.verifyEquals(false, isPresent, "验证没有流水记录");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProcessOrderPay_007() {
		Logger.start(true,"接收订单支付消息成功,积分增加-会员不存在自动创建会员");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			processorderpayBean.setBuyerId(userId);
			processorderpayBean.setSqOrderRmbAmount("150");
			processorderpayBean.setOrderId(orderid);
			processorderpayBean.setSellerId(shopid);
			processorderpayBean.setPayTime(YMTDateUtil.getBeforeOrNextDay(0));
			processorderpayBean.setGoodsOwnerId(shopid);
			processorderpayCall.setData(processorderpayBean);
			processorderpayCall.callService();			
			Logger.verifyEquals("ok", processorderpayCall.getReturnData(), "返回成功");
			List<Member> list = GodMemberWapper.selectMemberByUserId(userId, shopid);
			Logger.debug(FormatUtil.beanToGSONString(list));
			Logger.verifyEquals(1,list.size(), "验证创建了新用户");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProcessOrderPay_008() {
		Logger.start(true,"不输入paytime,不处理");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			processorderpayBean.setBuyerId(userId);
			processorderpayBean.setSqOrderRmbAmount("150");
			processorderpayBean.setOrderId(orderid);
			processorderpayBean.setSellerId(shopid);
//			processorderpayBean.setPayTime(YMTDateUtil.getBeforeOrNextDay(0));
			processorderpayBean.setGoodsOwnerId(shopid);
			processorderpayCall.setData(processorderpayBean);
			processorderpayCall.callService();			
			Logger.verifyEquals("ok", processorderpayCall.getReturnData(), "返回成功");
			
			List<MemberPoints> mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
			Logger.verifyEquals(0, mpdb.size(), "不输入paytime不落库");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			Boolean isPresent= Entrylist.stream().filter(x->{return x.getBizType().equals(100000);}).findFirst().isPresent();
			Logger.verifyEquals(false, isPresent, "验证没有流水记录");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProcessOrderPay_009() {
		Logger.start(true,"不输入GoodsOwnerId,不处理");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			processorderpayBean.setBuyerId(userId);
			processorderpayBean.setSqOrderRmbAmount("150");
			processorderpayBean.setOrderId(orderid);
			processorderpayBean.setSellerId(shopid);
			processorderpayBean.setPayTime(YMTDateUtil.getBeforeOrNextDay(0));
//			processorderpayBean.setGoodsOwnerId(shopid);
			processorderpayCall.setData(processorderpayBean);
			processorderpayCall.callService();			
			Logger.verifyEquals("ok", processorderpayCall.getReturnData(), "返回成功");
			
			List<MemberPoints> mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
			Logger.verifyEquals(0, mpdb.size(), "不输入paytime不落库");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			Boolean isPresent= Entrylist.stream().filter(x->{return x.getBizType().equals(100000);}).findFirst().isPresent();
			Logger.verifyEquals(false, isPresent, "验证没有流水记录");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}